<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		ScriptPlanet.eu															/
/	Site:			http://www.scriptplanet.eu												/
/	Email:			support@scriptplanet.eu													/
/	Support:		http://www.scriptplanet.eu/support										/
/	Copyright:		2011 (c) ScriptPlanet.eu												/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0001																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla Security
defined('_JEXEC') or die;
JHtml::_('behavior.tooltip');

$scriptMarge = 0;

?>
<div id="output"><input type="hidden" id="jsamLiveSid" value="0"/></div>
<script>
	
	// ID check

	function jsamLchkObject(){
		var req;
		if(window.XMLHttpRequest){
			req = new XMLHttpRequest();
		}
		else if(window.ActiveXObject){
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else{
			alert("Your browser is to old to show this page.");
		}
		return req;
	}
	var jsamLck = jsamLchkObject();
	
	function jsamLchkSend(url){
		jsamLck.open('post', url ,true);
		jsamLck.onreadystatechange = jsamLchkHandleResponse;
		jsamLck.send(null);
	}

	function jsamLchkHandleResponse(){
		if(jsamLck.readyState == 4 && jsamLck.status == 200){
			var target = document.getElementById('jsamLiveSid');
			var response = eval("(" + jsamLck.responseText + ")");

			if(response.state == 'playing' || response.state == 'offline'){
				var realid = response.songid;
				var docid = target.value;

				if(docid != realid){
					jsamLiveSend("<?php echo JRoute::_('index.php?option=com_joosam&view=data&layout=live&format=raw&check=full'); ?>");
				}
			}
			else{
				jsamLiveSend("<?php echo JRoute::_('index.php?option=com_joosam&view=data&layout=live&format=raw&check=full'); ?>");
			}
		}
	}

	// Output
	function jsamLiveObject(){
		var req;
		if(window.XMLHttpRequest){
			req = new XMLHttpRequest();
		}
		else if(window.ActiveXObject){
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else{
			alert("Your browser is to old to show this page.");
		}
		return req;
	}
	var jsamLiv = jsamLiveObject();

	function jsamLiveSend(url){
		jsamLiv.open('post', url ,true);
		jsamLiv.onreadystatechange = jsamLiveHandleResponse;
		jsamLiv.send(null);
	}

	function jsamLiveHandleResponse(){
		if(jsamLiv.readyState == 4 && jsamLiv.status == 200){
			var target = document.getElementById('output');
			var response = eval("(" + jsamLiv.responseText + ")");

			var imgdata = '';
			if(response.imgtype == 'passed' || response.imgtype == 'lastfmerr'){
				imgdata = response.imgdata[0];
			}
			else{
				imgdata = response.imgdata;
			}

			// Build Data's
			var picture;
			
			if(response.imgtype == 'passed'){
				picture = '<img src="'+imgdata+'" style="height:'+response.imgheight+';width:'+response.imgwidth+';border:none;margin:0;padding:0;" />';
			}
			else{
				picture = '<p class="jsamImgErr" style="height:'+response.imgheight+';width:'+response.imgwidth+';border:none;margin:0;padding:0;">'+imgdata+'</p>';
			}

			var hidden = '<input type="hidden" id="jsamLiveSid" value="'+response.songid+'"/>';
			var data = '	<div class="jsam_data">'+
								'<table border="0" class="jsam_table">'+
									'<tr class="jsam_row">'+
										'<td class="jsam_td_artist">'+response.lang_artist+':</td>'+
										'<td class="jsam_td_artist_data">'+response.artist+'</td>'+
									'</tr>'+
									'<tr class="jsam_row">'+
										'<td class="jsam_td_title">'+response.lang_title+':</td>'+
										'<td class="jsam_td_title_data">'+response.title+'</td>'+
									'</tr>'+
									'<tr class="jsam_row">'+
										'<th colspan="2" class="jsam_th_timer">'+
											'<div id="countje"></div>'+
										'</th>'+
									'</tr>'+
								'</table>'+
							'</div>';
							
			target.innerHTML = picture + hidden + data;
			if

			// Time calculator
			var hour = response.hour_left;
			var min = response.min_left;
			var sec = response.sec_left;
			var marge = "<?php echo $scriptMarge; ?>";
			hour = parseFloat(hour);
			min = parseFloat(min);
			sec = parseFloat(sec);
			marge = parseFloat(marge);

			setcountnow(hour, min, sec);
		}
	}

	function jsamLiveRefresh(){
		jsamLchkSend("<?php echo JRoute::_('index.php?option=com_joosam&view=data&layout=live&format=raw&check=idonly'); ?>");
		setTimeout('jsamLiveRefresh()', 2000);
	}
	window.onload = jsamLiveRefresh();

	
	function setcountnow(hour, min, sec){

		var hours = hour;
		var mins = min;
		var secs = sec;

		if(mins <= 0 && hours >= 1){
			mins = 60;
			hours = hours - 1;
		}
		if(secs <= 0 && mins >= 1){
			secs = 60;
			mins = mins - 1;
		}

		if(mins <= 0 && secs <= 0){

		}
		else{
			var data;
			secs = secs -1;
			if(secs < 10){
				secs = '0'+secs;
			}
			if(hours > 0){
				if(mins < 10){
					mins = '0'+mins;
				}
				data = hours + ':' + mins + ':' + secs;
			}
			else{
				data = mins + ':' + secs;
			}
			document.getElementById("countje").innerHTML = data;
			setTimeout("setcountnow('" + hours + "', '" + mins + "', '" + secs +"')",1000);
		}
	}
				
		
</script>

